/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;


import email.Email;
import core.User;
import javax.faces.event.ActionEvent;
import db.factories.UserDAOFactory;
import db.factories.UserDAOFactory.DAOTYPE;
import db.interfaces.UserDAO;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.mail.MessagingException;






/**
 *
 * @author Wizz
 */
public class LoginBean {

    boolean forgotPasswordPopup;
    private String password, email;
    private UserDAO dao;
    private SessionBean sessionBean;

    /**
     *
     */
    @PostConstruct
    public void init()
    {
            forgotPasswordPopup = false;
            dao = UserDAOFactory.getDAO(DAOTYPE.JDBC);

    }
    /**
     *
     * @return
     */
    public String getPassword()
    {
        return password;
    }
    /**
     *
     * @param confirmPassword
     */
    public void setPassword(String confirmPassword)
    {
        this.password = confirmPassword;
    }
    /**
     *
     * @return
     */
  
    public SessionBean getSessionBean()
    {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean)
    {
        this.sessionBean = sessionBean;
    }
     
    /**
     *
     * @return
     */
    public String getEmail()
    {
        return email;
    }
    /**
     *
     * @param email
     */
    public void setEmail(String email)
    {
        this.email = email;
    }
    /**
     *
     * @param e
     */
    public void submitLogin(ActionEvent e)
    {
        System.out.println("submitLogin");
    }
    /**
     *
     * @return
     * @throws ValidatorException
     */
    public String navigateLogin() throws ValidatorException
    {
        //Set the current User in the sessionbean. If u get here the login was successful.
        sessionBean.setCurrentUser(dao.getUserByEmail(email));
        
        System.out.println("navigateLogin");
        return "success";
    }
    /**
     *
     * @param context
     * @param validate
     * @param value
     * @throws ValidatorException
     */
    public void validateEmail(FacesContext context, UIComponent validate, Object value) throws ValidatorException{

        email = (String) value;
        if(email.indexOf('@')==-1){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("Invalid Email");
            context.addMessage(validate.getClientId(context), msg);
        }
        else if(!dao.containsUser(email)){

            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("Email does not exist. Please Register.");
            context.addMessage(validate.getClientId(context), msg);
        }

    }
    /**
     * 
     * @param context
     * @param validate
     * @param value
     * @throws ValidatorException
     */
    public void validatePassword(FacesContext context, UIComponent validate, Object value) throws ValidatorException
    {
        password = (String)value;
        User user2 = dao.getUserByEmail(email);

        if(user2 != null && !password.equals(user2.getPassword())){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("Wrong Password. Please try again.");
            context.addMessage(validate.getClientId(context), msg);
        }
        else if (password == null){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("Please supply a password.");
            context.addMessage(validate.getClientId(context), msg);
        }
    }
    /**
     *
     * @return
     */
    public boolean getForgotPasswordPopup()
    {
        return forgotPasswordPopup;
    }
    /**
     *
     * @param editMode
     */
    public void setForgotPasswordPopup(boolean editMode)
    {
        this.forgotPasswordPopup = editMode;
    }
    /**
     *
     * @param e
     */
    public void enableForgotPasswordPopup(ActionEvent e)
    {
        forgotPasswordPopup = true;
    }
    /**
     *
     * @param e
     */
    public void disableForgotPasswordPopup(ActionEvent e)
    {
        forgotPasswordPopup = false;
    }
    /**
     *
     * @param e
     * @throws MessagingException
     */
    public void sendEmail(ActionEvent e) throws MessagingException
    {
        User u = dao.getUserByEmail(email);
        forgotPasswordEmail(u);

        //Closing the popup window.
        forgotPasswordPopup = false;
    }

    private void forgotPasswordEmail(User u) throws MessagingException
    {
        String subject = "Your user information at www.bookSelector.se";
        String message =  "Here is your user information at www.bookSelector.se" + "\n" + "\n" +
                           "Username: " + u.getEmail() + "\n" +
                           "Password: " + u.getPassword() + "\n" + "\n" +
                           "Regards"+ "\n"+ "\n"+
                           "The bookSelector Crew";
        
        Email.sendMail(u.getEmail(), subject, message);

    }
}
